<template>
  <div class="Swiper">
    <el-card class="swiper-container">
      <el-table
        border
        v-loading="loading"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="轮播图" width="200">
          <template #default="scope">
            <img
              style="width: 150px; height: 150px"
              :src="scope.row.carouselUrl"
              alt="轮播图"
            />
          </template>
        </el-table-column>
        <el-table-column label="跳转链接">
          <template #default="scope">
            <a target="_blank" :href="scope.row.redirectUrl">{{
              scope.row.redirectUrl
            }}</a>
          </template>
        </el-table-column>
        <el-table-column prop="carouselRank" label="排序值" width="120">
        </el-table-column>
        <el-table-column prop="createTime" label="添加时间" width="200">
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :page-size="pageSize"
        :current-page="currentPage"
        @current-change="changePage"
      />
      <template #header>
        <el-card class="swiper-container">
          <div class="header">
            <el-button
              type="primary"
              size="small"
              icon="el-icon-plus"
              @click="handleAdd"
              >增加</el-button
            >
            <el-popconfirm
              title="确定删除吗？"
              confirmButtonText="确定"
              cancelButtonText="取消"
              @confirm="handleDelete"
            >
              <template #reference>
                <el-button type="danger" size="small" icon="el-icon-delete"
                  >批量删除</el-button
                >
              </template>
            </el-popconfirm>
          </div>
        </el-card>
        <DialogAddSwiper ref="addSwiper" :reload="getCarousels" :type="type" />
      </template>
    </el-card>
  </div>
</template>
<script>
import { ref, toRefs, reactive, defineComponent, onMounted } from "vue";
import { useRouter, useRoute } from "vue-router";
import DialogAddSwiper from "@/components/DialogAddSwiper.vue";
import axios from "@/utils/axios";
import { ElMessage } from "element-plus";
export default defineComponent({
  name: "Swiper",
  components: { DialogAddSwiper },
  setup() {
    const router = useRouter();
    const route = useRoute();
    const addSwiper = ref(null);
    const state = reactive({
      loading: false,
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      type: "add",
      multipleSelection: [],
    });

    onMounted(() => {
      getCarousels();
    });
    // 获取轮播图列表
    const getCarousels = () => {
      state.loading = true
      axios.get('/api/carousels', {
        params: {
          pageNumber: state.currentPage,
          pageSize: state.pageSize
        }
      }).then(res => {
        state.tableData = res.list
        state.total = res.totalCount
        state.currentPage = res.currPage
        state.loading = false
      })
    };
    // 添加轮播项
    const handleAdd = () => {
      state.type = "add";
      addSwiper.value.open();
    };
    // 修改轮播图
    const handleEdit = (id) => {
      state.type = "edit";
      addSwiper.value.open(id);
    };
    // 选中之后的change方法，一旦选项有变化，就会触发该方法。
    const handleSelectionChange = (val) => {
      state.multipleSelection = val;
    };
    // 批量删除
    const handleDelete = () => {
      if (!state.multipleSelection.length) {
        ElMessage.error("请选择项");
        return;
      }
      axios
        .delete("/carousels", {
          data: {
            ids: state.multipleSelection.map((i) => i.carouselId),
          },
        })
        .then(() => {
          ElMessage.success("删除成功");
          getCarousels();
        });
    };
    const changePage = (val) => {
      state.currentPage = val
      getCarousels()
    }
    return {
      ...toRefs(state),
      addSwiper,
      handleAdd,
      handleEdit,
      getCarousels,
      handleSelectionChange,
      handleDelete,
      changePage,
    };
  },
});
</script>
<style scoped>
.Swiper {
}
</style>
